package com.xuecheng.content.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.execption.CommonError;
import com.xuecheng.base.execption.XueChengPlusException;
import com.xuecheng.base.model.Result;
import com.xuecheng.base.model.ResultUtil;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 课程-教师关系表 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@RequestMapping
public class CourseTeacherController {

    @Autowired
    private CourseTeacherService  courseTeacherService;


    /**
     * 查询教师信息
     * @author hjh
     * @date 2023/5/7 11:45
     * @param courseId
     * @return CourseTeacher
     */

    @GetMapping("/courseTeacher/list/{courseId}")
    @ApiOperation("通过课程id获取教师信息列表")
    public List<CourseTeacher> getCourseTeacher(@PathVariable @NotEmpty(message = "课程id不能为空")Long courseId){
        return courseTeacherService.getCourseTeacher(courseId);
    }

    /**
     * 添加教师信息
     * @author hjh
     * @date 2023/5/7 17:51
     * @param courseTeacher
     * @return CourseTeacher
     */

    @PostMapping("/courseTeacher")
    @ApiOperation("添加教师信息")
    @Description("添加教师信息")
    public CourseTeacher addCourseTeacher(@RequestBody @Validated CourseTeacher courseTeacher){

        final String teacherName = courseTeacher.getTeacherName();
        CourseTeacher one = courseTeacherService.getOne(new LambdaQueryWrapper<CourseTeacher>().eq(CourseTeacher::getTeacherName, teacherName));
        if(one ==null){
            courseTeacher.setCreateDate(LocalDateTime.now());
            boolean save = courseTeacherService.save(courseTeacher);
            if(!save) {
                XueChengPlusException.cast(CommonError.UNKOWN_ERROR);
            }
        }else {
            XueChengPlusException.cast("教师已存在，不能添加");
        }
        return courseTeacher;
    }


    /**
     * 修改教师信息
     * @author hjh
     * @date 2023/5/7 17:54
     * @param courseTeacher
     * @return CourseTeacher
     */

    @PutMapping("/courseTeacher")
    @ApiOperation("修改教师信息")
    public CourseTeacher modifyCourseTeacher(@RequestBody  CourseTeacher courseTeacher){
        String teacherName = courseTeacher.getTeacherName();
        CourseTeacher teacher = courseTeacherService.getOne(new LambdaQueryWrapper<CourseTeacher>().eq(CourseTeacher::getTeacherName, teacherName));
        if(teacher!=null){
            XueChengPlusException.cast("教师姓名已存在，请更改");
        }
        courseTeacherService.updateById(courseTeacher);
        return courseTeacher;
    }

    @Description("删除教师信息，第一个参数为课程id，第二个参数为教师id")
    @DeleteMapping("/courseTeacher/course/{courseId}/{teacherId}")
    public Result<Object> deleteCourseTeacher(@PathVariable @NotEmpty(message = "课程id不能为空") Long courseId,@PathVariable @NotEmpty(message = "教师id不能为空") Long teacherId){
        boolean remove = courseTeacherService.remove(new LambdaQueryWrapper<CourseTeacher>().eq(CourseTeacher::getCourseId, courseId).eq(CourseTeacher::getId, teacherId));
        if (remove){
            return ResultUtil.success("删除成功");
        }else{
            XueChengPlusException.cast("删除失败");
        }
        return ResultUtil.error("删除失败");
    }
}
